package cn.gdk.gaodekecc.service.impl;

import cn.gdk.gaodekecc.domain.MoveUser;
import cn.gdk.gaodekecc.domain.MovieTicket;
import cn.gdk.gaodekecc.exception.GlobalExceptionCodeEnum;
import cn.gdk.gaodekecc.mapper.MoveUserMapper;
import cn.gdk.gaodekecc.mapper.MovieTicketMapper;
import cn.gdk.gaodekecc.result.JSONResult;
import cn.gdk.gaodekecc.service.IMoveUserService;
import cn.gdk.gaodekecc.service.IMovieTicketService;
import cn.gdk.gaodekecc.util.AssertUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author director
 * @since 2022-11-25
 */
@Service
public class MovieTicketServiceImpl extends ServiceImpl<MovieTicketMapper, MovieTicket> implements IMovieTicketService {
   @Autowired
   private IMoveUserService moveUserService;

    /**
     * 买票
     * @param movieId
     * @param count
     * @return
     */
    @Override
    public JSONResult buy(Long movieId, Integer count) {
        // 查询电影票
        MovieTicket movieTicket = selectById(movieId);
        AssertUtil.isNotNull(movieTicket, GlobalExceptionCodeEnum.COMM_MOVIE_TICKETS_SOLD_OUT);
        movieTicket.setStock(movieTicket.getStock()-count);
        updateById(movieTicket);
        //中间表
        EntityWrapper<MoveUser> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id",1L);
        wrapper.eq("movie_ticket_id",movieId);
        MoveUser moveUser = moveUserService.selectOne(wrapper);
        MoveUser moveUser1 = new MoveUser();
        moveUser1.setUserId(1L);
        moveUser1.setMovieTicketId(movieId);
        moveUser1.setCount(count);
        moveUserService.insert(moveUser1);
        return JSONResult.success();
    }
}
